/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.IMAPMIDlet;
import core.RuntimeData;
import core.ScreenEngine;
import network.Connection;
import network.PacketBuilder;
import view.additional.CellItem;
import view.additional.MenuList;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class ExtraFunctionsListForm extends BasicForm implements MenuList.MenuListSelection
{
    private static final int OK_COMMAND_ID = 1;
    
    private static final int CHECK_BALANCE_ITEM_ID = 0;
    
    private MenuList items;
    
    public ExtraFunctionsListForm()
    {
        super("extra_func");
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setScrollable(false);
        
        items = new MenuList();
        items.addItem(new CellItem(CHECK_BALANCE_ITEM_ID, "check_balance"));
        items.setSelectionListener(this);
        addComponent(items);
        
        //Команды
        addCommand(new BackCommand());
        
        Command ok = new Command("ok", OK_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                itemSelected(items.getSelectedId());
                
                evt.consume();
            }
        };
        addCommand(ok);
    }
    
    public void itemSelected(int id)
    {
        switch(id)
        {
            case CHECK_BALANCE_ITEM_ID : requestBalance(); break;
        }
    }
    
    private void requestBalance()
    {
        //Формируем и отправляем запрос
        RuntimeData runtimeData = IMAPMIDlet.getMidlet().getRuntimeData();
        byte[] body = PacketBuilder.createGetBalanceData(runtimeData.getPeopleID());
	byte[] data = PacketBuilder.createSrvTransfereData(PacketBuilder.DEFAULT_CONNECTION_TYPE, runtimeData.getSrvID(), 
                                                           PacketBuilder.DEFAULT_DESTINATION_ID, runtimeData.getGUID(), true, body);
        Connection.getConnection().send(data);
                
        //Возвращаемся на главную форму
        ScreenEngine.getEngine().popTo(0);        
    }
}
